package com.ay.weixin.autoreply.db;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import lizar.db.ibatis.Dao;

import org.lizar.config.keys.Keys;

import com.ay.weixin.autoreply.domain.Message;

public class IbatisAutoReplyDao extends Dao{

	public void insert(Message message) throws SQLException{
		this.sqlMapper.insert("auto_reply.insert",message);
	}
	
	public void insert_with_parent_id(Message message) throws SQLException{
		this.sqlMapper.insert("auto_reply.insert_with_parent_id",message);
	}
	
	public Message getEventMessage(long wxproject_id,String event) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("event", event);
		keys.put("msg_type", "event");
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getEventMessage",keys);
		
	}
	
	public Message getEventMessage(long wxproject_id,String event,String msg_type) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("event", event);
		keys.put("msg_type",msg_type);
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getEventMessage",keys);
		
	}
	
	public void update_keyword(Message message) throws SQLException{
		System.out.println("parent_id:"+message.getParent_id());
		this.sqlMapper.update("auto_reply.update_keyword",message);
	}
	
	
	public Map<Long,Message> getKeyWords(long wxproject_id) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("event", "input");
		keys.put("msg_type", "text");
		keys.put("wxproject_id", wxproject_id);
		return (Map<Long,Message>)this.sqlMapper.queryForMap("auto_reply.getKeyWords",keys,"id");
	}
	
	public List<Message> getKeyWordsList(long wxproject_id) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("event", "input");
		keys.put("msg_type", "text");
		keys.put("wxproject_id", wxproject_id);
		return (List<Message>)this.sqlMapper.queryForList("auto_reply.getKeyWords",keys);
	}
	
	public void update_response_type(String type,long id) throws SQLException{
		Map<String,String> keys=new LinkedHashMap<String,String>();
		keys.put("response_type", type);
		keys.put("id", id+"");
		this.sqlMapper.update("auto_reply.update_response_type",keys);
	}
	
	public void update_text(Message message) throws SQLException{
		this.sqlMapper.update("auto_reply.update_text",message);
	}
	
	public void delete_by_id(long id) throws SQLException{
		this.sqlMapper.delete("auto_reply.delete_by_id",id);
	}
	
	public Message getCLICKMessage(long wxproject_id,String click_match_type,String click_key) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("event", Keys.getStr("input_message_event_type.CLICK"));
		keys.put("msg_type", "event");
		keys.put("match_type", click_match_type);
		keys.put("input", click_key);
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getCLICKMessage1",keys);
	}
	
	public void delete_click_message(long wxproject_id,String click_match_type,String click_key,String response_type) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("event", Keys.getStr("input_message_event_type.CLICK"));
		keys.put("msg_type", "event");
		keys.put("match_type", click_match_type);
		keys.put("input", click_key);
		keys.put("response_type", response_type);
		keys.put("wxproject_id", wxproject_id);
		this.sqlMapper.delete("auto_reply.delete_click_message",keys);
	}
	
	public Message getMessage(long id) throws SQLException{
		return (Message)this.sqlMapper.queryForObject("auto_reply.getMessage",id);
		
	}
	
	public Message getCLICKMessage(long wxproject_id,String click_match_type,String click_key,String response_type) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("event", Keys.getStr("input_message_event_type.CLICK"));
		keys.put("msg_type", "event");
		keys.put("match_type", click_match_type);
		keys.put("input", click_key);
		keys.put("response_type", response_type);
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getCLICKMessage2",keys);
		
	}
	

	
	public Message getMatchedInput(long wxproject_id,String input) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("input", input);
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getMatchedInput",keys);
	}
	
	public Message getTopMatchedInput(long wxproject_id,String input) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("input", input);
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getTopMatchedInput",keys);
	}
	
	public Message getParentMatchedInput(long wxproject_id,String input,long parent_id) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("input", input);
		keys.put("parent_id", parent_id);
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getParentMatchedInput",keys);
	}

	
	public Message getById(long id) throws SQLException{
		return (Message)this.sqlMapper.queryForObject("auto_reply.getById",id);
	}
	
	public boolean hasChildren(long id) throws SQLException{
		List l=this.sqlMapper.queryForList("auto_reply.hasChildren",id);
		if(l==null||l.isEmpty())return false;
		return true;
	}
	
	public Message getDefaultTextMsgReply(long wxproject_id) throws SQLException{
		Map<String,Object> keys=new LinkedHashMap<String,Object>();
		keys.put("msg_type", "text");
		keys.put("event", "default_msg_reply");
		keys.put("wxproject_id", wxproject_id);
		return (Message)this.sqlMapper.queryForObject("auto_reply.getDefaultTextMsgReply",keys);
	}
	
	
	
}
